<template>
  <div>
    <div v-if="searchType==='0'" :class="changeTheme">
      <van-icon name="search" class="fs-32" />
      <input
        v-model="val"
        class="pl-20"
        type="text"
        :placeholder="placeholder"
        @keyup.enter="search"
        @input="changeVal"
      >

      <van-icon v-show="val" name="close" class="fs-32" @click="clear" />
      <button class="bg-tp flex-s-0 br-32 plr-45 fs-26" @click="search">
        <span>搜索</span>
      </button>
    </div>
    <div v-if="searchType==='1'" class="br-32 flex-s-c fs-24 pt-30 type1">
      <div class="pos-r flex-1">
        <van-icon name="search" class="fs-32 pos-a left-20 top-20" />
        <input
          v-model="val"
          type="text"
          :placeholder="placeholder"
          @keyup.enter="search"
          @input="changeVal"
        >
        <van-icon v-show="val" name="close" class="fs-32 pos-a top-20 right-10" @click="clear" />
      </div>
      <button class="bg-tp flex-s-0 br-32 plr-30 fs-26" @click="search">
        <span>搜索</span>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ContentSearch',
  props: {
    placeholder: {
      type: String,
      required: true,
      default: '请输入搜索关键字'
    },

    theme: {
      type: String,
      default: '0'
    },
    searchVal: {
      type: String,
      default: undefined
    },
    searchType: {
      type: String,
      default: '0'
    }

  },
  data() {
    return {
      val: ''
    }
  },
  computed: {
    changeTheme() {
      if (this.theme) {
        return 'br-32 flex-s-c fs-24 pl-25 mt-30 input-theme-' + this.theme
      } else {
        return 'br-32 flex-s-c fs-24 pl-25 mt-30 '
      }
    }
  },
  watch: {
    searchVal() {
      // 监听父组件的变化
      this.val = this.searchVal
    }
  },
  methods: {
    changeVal() {
      this.$emit('getVal', this.val)
    },
    clear() {
      this.$emit('clear')
    },

    search() {
      this.$emit('search', this.val)
    }
  }
}
</script>
